package com.abss.vote.service.impl;

import com.abss.vote.mapper.CommentMapper;
import com.abss.vote.model.*;
import com.abss.vote.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 获取评论列表
     * @param work_id
     * @return
     */
    @Override
    public List<Comment> getComments(int start, int end,Integer work_id) {
        //首先获取该作品下所有的未删除的一级评论
        List<Comment> comments = commentMapper.getOneComment(start, end,work_id);
        if(comments==null||comments.size()==0){//没有评论
            return new ArrayList<>();
        }
        for (Comment comment : comments) {//遍历所有一级评论
            //获取评论者的姓名头像
            String user_id  = comment.getUSER_ID();
            Comment user = new Comment();
            if(user_id.substring(0,10).equals("feiyuebook")){
                user = commentMapper.getAdmin(user_id.substring(11,17),user_id.substring(18,user_id.length()));
            }else {
                user = commentMapper.getUser(user_id);
            }
            comment.setNICKNAME(user.getNICKNAME());
            comment.setHEAD_IMG_URL(user.getHEAD_IMG_URL());
            List<Comment> parents = commentMapper.getTwoComment(work_id,comment.getID());//获取所有二级评论
            for(Comment comment1:parents){
                //将被回复的人的名字头像放入
                comment1.setBE_NICKNAME(user.getNICKNAME());
                comment1.setBE_HEAD_IMG_URL(user.getHEAD_IMG_URL());
                String user_id1  = comment1.getUSER_ID();
                Comment user1 = new Comment();
                if(user_id1.substring(0,10).equals("feiyuebook")){
                    user1 = commentMapper.getAdmin(user_id1.substring(11,17),user_id1.substring(18,user_id1.length()));
                }else {
                    user1 = commentMapper.getUser(user_id1);
                }
                comment1.setNICKNAME(user1.getNICKNAME());
                comment1.setHEAD_IMG_URL(user1.getHEAD_IMG_URL());
            }
            comment.setChild(parents);
        }
        return comments;
    }

    @Override
    public int insertComment(Comment comment) {
        //首先插入评论
        return commentMapper.insertComment(comment);
    }

    @Override
    public Boolean IsAuthor(Comment comment) {
        Work work = commentMapper.IsAuthor(comment);
        if(work==null){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public int insertReply(Comment comment) {
        //获取被回复者的user_id
        String USER_ID = commentMapper.getBe_User_ID(comment.getPARENT_ID());
        comment.setBE_USER_ID(USER_ID);
        return commentMapper.insertReply(comment);
    }

    @Override
    public String getWorkName(Integer work_id) {
        return commentMapper.getWorkName(work_id);
    }

    @Override
    public PageInfo<Comment> getCommentsBack(int start, int end, Integer work_id) {
        PageHelper.startPage(start,end);
        List<Comment> comments = commentMapper.getOneCommentBack(work_id);
        List<Comment> lists = new ArrayList<>();
        for(Comment comment:comments){
            String user_id  = comment.getUSER_ID();
            Comment user = new Comment();
            if(user_id.substring(0,10).equals("feiyuebook")){
                user = commentMapper.getAdmin(user_id.substring(11,17),user_id.substring(18,user_id.length()));
            }else {
                user = commentMapper.getUser(user_id);
            }
            comment.setNICKNAME(user.getNICKNAME());
            comment.setHEAD_IMG_URL(user.getHEAD_IMG_URL());
            lists.add(comment);
            List<Comment> parents = commentMapper.getTwoComment(work_id,comment.getID());//获取所有二级评论
            for(Comment comment1:parents){
                //将被回复的人的名字头像放入
                comment1.setBE_NICKNAME(user.getNICKNAME());
                comment1.setBE_HEAD_IMG_URL(user.getHEAD_IMG_URL());
                String user_id1  = comment1.getUSER_ID();
                Comment user1 = new Comment();
                if(user_id1.substring(0,10).equals("feiyuebook")){
                    user1 = commentMapper.getAdmin(user_id1.substring(11,17),user_id1.substring(18,user_id1.length()));
                }else {
                    user1 = commentMapper.getUser(user_id1);
                }
                comment1.setNICKNAME(user1.getNICKNAME());
                comment1.setHEAD_IMG_URL(user1.getHEAD_IMG_URL());
                lists.add(comment1);
            }
        }
        PageInfo<Comment> list= new PageInfo<>(lists);
        return list;
    }

    @Override
    public int getCount(Integer work_id) {
        return commentMapper.getCount(work_id);
    }

    @Override
    public int setCommentBack(String conent, Integer work_id, Administrators ad) {
        Comment comment = new Comment();
        comment.setCONENT(conent);
        comment.setUSER_ID("feiyuebook-"+ad.getSERVER_ID()+"-"+ad.getADMIN());
        comment.setWORK_ID(work_id);
        return commentMapper.setCommentBack(comment);
    }

    @Override
    public int setReplyBack(String conent, Integer work_id, Administrators ad,Integer id,String BE_USER_ID) {
        Comment comment = new Comment();
        comment.setCONENT(conent);
        comment.setUSER_ID("feiyuebook-"+ad.getSERVER_ID()+"-"+ad.getADMIN());
        comment.setWORK_ID(work_id);
        comment.setPARENT_ID(id);
        comment.setBE_USER_ID(BE_USER_ID);
        return commentMapper.setReplyBack(comment);
    }
}
